package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/25 22:15
 */
public class 统计数组中峰和谷的数量 {
    //https://leetcode.cn/problems/count-hills-and-valleys-in-an-array/
    public int countHillValley(int[] nums) {
        int r = 1;
        int res = 0;
        for (int i = 1; i < nums.length - 1; i++) {
            while (r < nums.length && nums[i] == nums[r]) {
                r++;
            }
            if (r == nums.length)
                break;
            if ((nums[i] > nums[i - 1] && nums[i] > nums[r]) || (nums[i] < nums[i - 1] && nums[i] < nums[r]))
                res++;
            i = r - 1;
        }
        return res;
    }

    public static void main(String[] args) {
        统计数组中峰和谷的数量 s = new 统计数组中峰和谷的数量();
        int[] nums = {2, 4, 1, 1, 6, 5};
        System.out.println(s.countHillValley(nums));
    }
}
